import os.path as osp

from qgis.core import QgsMapLayer, QgsRasterLayer, QgsVectorLayer, QgsProject
from qgis.gui import QgsMapCanvas

PROJECT = QgsProject.instance()


# 添加图层到地图画布
def addMapLayer(layer: QgsMapLayer, mapCanvas: QgsMapCanvas, firstAddLayer=False):
    if layer.isValid():
        if firstAddLayer:
            mapCanvas.setDestinationCrs(layer.crs())
            mapCanvas.setExtent(layer.extent())

        while (PROJECT.mapLayersByName(layer.name())):
            layer.setName(layer.name() + "_1")

        PROJECT.addMapLayer(layer)
        layers = [layer] + [PROJECT.mapLayer(i) for i in PROJECT.mapLayers()]
        mapCanvas.setLayers(layers)
        mapCanvas.refresh()


# 读取栅格文件
def readRasterFile(rasterFilePath):
    rasterLayer = QgsRasterLayer(rasterFilePath, osp.basename(rasterFilePath))
    return rasterLayer


# 输出栅格图层属性
def getRasterLayerAttrs(rasterLayer: QgsRasterLayer):
    print("name: ", rasterLayer.name())  # 图层名
    print("type: ", rasterLayer.type())  # 栅格还是矢量图层
    print("height - width: ", rasterLayer.height(), rasterLayer.width())  # 尺寸
    print("bands: ", rasterLayer.bandCount())  # 波段数
    print("extent", rasterLayer.extent())  # 外接矩形范围
    print("source", rasterLayer.source())  # 图层的源文件地址
    print("crs", rasterLayer.crs())  # 图层的坐标系统


# if __name__ == '__main__':
#     tifPath = r"C:\Users\swdzz\Desktop\检测\天安门.tif"
#     tifLayer = readRasterFile(tifPath)
#     getRasterLayerAttrs(tifLayer)


# 读取矢量文件
def readVectorFile(vectorFilePath):
    vectorLayer = QgsVectorLayer(vectorFilePath, osp.basename(vectorFilePath), "ogr")
    return vectorLayer


# 输出矢量图层属性
def getVectorLayerAttrs(vectorLayer: QgsVectorLayer):
    print("name: ", vectorLayer.name())  # 图层名
    print("type: ", vectorLayer.type())  # 栅格还是矢量图层
    print("extent", vectorLayer.extent())  # 外接矩形范围
    print("source", vectorLayer.source())  # 图层的源文件地址
    print("crs", vectorLayer.crs())  # 图层的坐标系统

# if __name__ == '__main__':
#     shpPath = r"F:\三亚1\村庄合并4326\村庄合并4326\hebing.shp"
#     shpLayer = readVectorFile(shpPath)
#     getVectorLayerAttrs(shpLayer)
